package com.wanli.agent.homepage;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

import com.google.gson.Gson;
import com.wanli.agent.R;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.ChannelTypeBean;
import com.wanli.agent.bean.ExtensionCodeBean;
import com.wanli.agent.bean.MerchantIndustryNumberBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.utils.GlideUtils;
import com.wanli.agent.utils.QRCodeUtil;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.SaveImgTools;
import com.wanli.agent.utils.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExtensionCodeActivity extends BaseActivity {
    @BindView(R.id.iv_extension_code)
    ImageView iv_extension_code;
    @BindView(R.id.tv_commit)
    TextView tv_commit;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_extension_code)
    LinearLayout ll_extension_code;
    @BindView(R.id.tv_channel_type)
    TextView tv_channel_type;

    @BindView(R.id.tv_fl)
    TextView tv_fl;

    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extension_code);
        ButterKnife.bind(this);
        initView();
        initData();
        initEvent();
    }

    public void initView() {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        int id = userInfoBean.getData().getId();
        tvName.setText(userInfoBean.getData().getName() + " 邀请您");
    }

    public void initData() {

        new HomePageModelImpl().agentCode(new DataCallBack<ExtensionCodeBean>() {
            @Override
            public void onSuccessful(ExtensionCodeBean bean) {

                Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(bean.getData(), 480, 480);
                iv_extension_code.setImageBitmap(mBitmap);
                //获取服务商类型
                getAgentType(bean.getData());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void initEvent() {
        tv_commit.setOnClickListener(v -> {
            SaveImgTools.SaveViewToSysAlbum(ExtensionCodeActivity.this, ll_extension_code, userInfoBean.getData().getName());
        });
    }

    public void getAgentType(String qr_code_data) {
        new HomePageModelImpl().requestGetAgentType(qr_code_data, new DataCallBack<ChannelTypeBean>() {
            @Override
            public void onSuccessful(ChannelTypeBean bean) {

                if (bean.getData().getChannel_type() == 2) {
                    tv_fl.setText("注意：拓展码用于拓展商户，商户可识别拓展码进行自注册 默认费率为0.5%");
                    tv_channel_type.setBackground(getDrawable(R.drawable.shape_rectangle_conner_lanr5));
                    tv_channel_type.setText("智能版");
                } else {
                    tv_fl.setText("注意：拓展码用于拓展商户，商户可识别拓展码进行自注册 默认费率为0.36%");
                    tv_channel_type.setText("标准版");
                    tv_channel_type.setBackground(getDrawable(R.drawable.shape_rectangle_conner_lanr5));
                }

            }
            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }


}